﻿using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace LanqiQAwebapi.Models
{
    public partial class LanQiQaDBContext : DbContext
    {
        public LanQiQaDBContext()
        {
        }

        public LanQiQaDBContext(DbContextOptions<LanQiQaDBContext> options)
            : base(options)
        {
        }


        public virtual DbSet<Comment> Comments { get; set; } = null!;
        public virtual DbSet<Follow> Follows { get; set; } = null!;
        public virtual DbSet<Issue> Issues { get; set; } = null!;
        public virtual DbSet<Qa> Qas { get; set; } = null!;
        public virtual DbSet<User> Users { get; set; } = null!;

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 配置 Comment 实体
            modelBuilder.Entity<Comment>(entity =>
            {
                entity.HasKey(e => e.CommentId);
                entity.Property(e => e.Comment1).HasColumnName("Comment");
                entity.Property(e => e.UpvoteCount).HasDefaultValue(0);

                entity.HasOne(d => d.Answer)
                      .WithMany(p => p.Comments)
                      .HasForeignKey(d => d.AnswerId)
                      .OnDelete(DeleteBehavior.Cascade);

                entity.HasOne(d => d.User)
                      .WithMany(p => p.Comments)
                      .HasForeignKey(d => d.UserId)
                      .OnDelete(DeleteBehavior.SetNull);
            });


            // 配置 Follow 实体
            modelBuilder.Entity<Follow>(entity =>
            {
                entity.HasKey(e => e.FollowId);

                entity.HasOne(d => d.User)
                      .WithMany(p => p.FollowUsers)
                      .HasForeignKey(d => d.UserId)
                      .OnDelete(DeleteBehavior.ClientSetNull);

                entity.HasOne(d => d.FollowedUser)
                      .WithMany(p => p.FollowFollowedUsers)
                      .HasForeignKey(d => d.FollowedUserId)
                      .OnDelete(DeleteBehavior.ClientSetNull);
            });



            // 配置 Issue 实体
            modelBuilder.Entity<Issue>(entity =>
            {
                entity.HasKey(e => e.QuistionId);
                entity.ToTable("Issue");
                entity.Property(e => e.QuistionName).HasColumnName("QuistionName");
                entity.Property(e => e.Questioner).HasMaxLength(100);
                entity.Property(e => e.ProblemDescription).HasColumnType("ntext");
                entity.Property(e => e.QuestionTime).HasDefaultValueSql("GETDATE()");

                entity.HasOne(d => d.User)
                      .WithMany(p => p.Issue)
                      .HasForeignKey(d => d.UserId)
                      .OnDelete(DeleteBehavior.SetNull);

                entity.HasOne(d => d.Qa)
                      .WithMany(p => p.Questions)
                      .HasForeignKey(d => d.QaID)
                      .OnDelete(DeleteBehavior.SetNull);
            });


            // 配置 Qa 实体
            modelBuilder.Entity<Qa>(entity =>
            {
                entity.HasKey(e => e.QaId);

                entity.Property(e => e.AgreeCount).HasDefaultValue(0);

                entity.HasOne(d => d.User)
                      .WithMany(p => p.Qas)
                      .HasForeignKey(d => d.UserId)
                      .OnDelete(DeleteBehavior.SetNull);
            });



            // 配置 User 实体
            modelBuilder.Entity<User>(entity =>
            {
                entity.HasKey(e => e.UserId);

                entity.Property(e => e.UserName).HasMaxLength(50);
                entity.Property(e => e.Password).IsRequired().HasMaxLength(256); // 建议哈希存储
                entity.Property(e => e.Phone).HasMaxLength(15);
                entity.Property(e => e.AvatarPath).HasMaxLength(500);
                entity.Property(e => e.UserVip).HasDefaultValue(0);
            });

            // 避免表名复数化（保持单数形式）
            foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            {
                entityType.SetTableName(entityType.DisplayName());
            }

            OnModelCreatingPartial(modelBuilder);


        }
        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

    }
}
